﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using douban.app.Views;
using douban.Core.Apis;

namespace douban.Core.ViewModels
{
    public class ExploreVm:VmBase,IPivotItemContent
    {
        private int _start = 0;
        private int _count = 20;

        private Topic _selectedTopic;
        
        public ObservableCollection<Topic> ExploreTopics  { get; set; }

        public  ExploreVm()
        {
            ExploreTopics = new ObservableCollection<Topic>();
        }

        public Topic SelectedTopic
        {
            get { return _selectedTopic; }
            set
            {
                _selectedTopic = value; OnPropertyChanged();

                Navigate(typeof (TopicPage), _selectedTopic);
            }
        }


        /// <summary>
        /// 填充下一页数据
        /// </summary>
        private async void LoadNextPage()
        {
            var topics = await Apis.Api.GpSearchExploreTopics(_start,_count);
            foreach (var topic in topics)
            {
                ExploreTopics.Add(topic);
            }
            _start += _count;
        }


        public override void OnNavigatedFrom(NavigationEventArgs args)
        {
        }

        public override async void OnNavigatedTo(NavigationEventArgs args)
        {
            
        }

        /// <summary>
        /// 加载第一页数据
        /// </summary>
        public void LoadFirstPage()
        {
            if (_start == 0)
            {
                LoadNextPage();
            }
        }
    }
}
